import Vue from 'vue';
import {
  Message
} from 'element-ui';
import {
  MessageBox
} from 'element-ui';
import {
  Loading
} from 'element-ui';

module.exports = {
  post: function (url, params, success) {
    if (arguments.length === 0) {
      return;
    }
    if (arguments.length === 2) {
      if (typeof arguments[1] === 'function') {
        params = new Object();
        success = arguments[1];
      }
    }
    if (params === undefined || params === null) {
      params = new Object();
    }
    let loadingInstance = Loading.service();
    setTimeout(() => {
      loadingInstance.close();
    }, 300);

  },
  get: function (url, success) {
    let loadingInstance = Loading.service();
    setTimeout(() => {
      loadingInstance.close();
    }, 300);
  },
  trace: function (obj) {
    console.log(JSON.stringify(obj, null, 4));
  },
  confirmDialog: function (message, confirm) {
    MessageBox.confirm(message, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      confirm();
    }).catch(() => {
      Message.info('已取消');
    });
  },
  message: function (message, type) {
    if (arguments.length === 0) {
      return;
    }
    if (arguments.length === 1) {
      Message({
        showClose: true,
        message: message
      });
      return;
    }
    Message({
      showClose: true,
      message: message,
      type: type
    });
  },
  formatterTimestamp: function (timestamp) {
    if (timestamp !== null) {
      let date = new Date(timestamp);
      return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "  " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
    } else {
      return "";
    }
  },
  getPermission(url) {
    let b = false;
    let GET_PERMISSION_LIST = JSON.parse(window.sessionStorage.getItem("listPermissionFuns"));
    if (GET_PERMISSION_LIST != null) {
      for (let c of GET_PERMISSION_LIST) {
        if (url == c.url) {
          return true;
        } else {
          b = false;
        }

      }
      return b;
    } else {
      return false;
    }
  }
}
